package org.springframework.samples.mvc.basic.gotovproizvod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.mvc.basic.controller.CRUDController;
import org.springframework.samples.mvc.basic.model.UIFormModel;
import org.springframework.samples.mvc.basic.pagination.Pagination;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/gotovProizvodView")
public class GotovProizvodController extends CRUDController<GotovProizvodView> {

	@Autowired
	public GotovProizvodController(GotovProizvodBusinessService service) {
		super(service);
	}

	@Override
	protected Class<GotovProizvodView> getPojoClass() {
		return GotovProizvodView.class;
	}

	@Override
	protected GotovProizvodView createNewObject() {
		return new GotovProizvodView();
	}

	@Override
	protected String getLowerCapitalizedClassName() {
		return "gotovProizvodView";
	}

	@Override
	protected UIFormModel[] getDetailModels() {
		return new UIFormModel[0];
	}
	
	@RequestMapping("/{id}/details")
    public @ResponseBody Map<String, Object> get(@PathVariable Long id, @RequestParam(defaultValue = "1") Integer pg) {
		GotovProizvodBusinessService businessService = (GotovProizvodBusinessService) getBusinessService();
		Map<String, Object> result = new HashMap<String, Object>();

		Pagination pagination = addPagination(result, pg, businessService.countByEdinicaMerka(id));
		List<GotovProizvodView> res = businessService.getByEdinicaMerkaId(pagination.getStartIndex(), PAGE_SIZE, id);
    	
    	putModel(result);
        putObjectsInMap(result, res);
    	return result;
    }
	
}
